package com.rambler.extension.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rambler.common.response.CommonResponse;
import com.rambler.extension.entity.ExtEngineUserRel;
import com.rambler.extension.entity.ExtSearchEngine;
import com.rambler.extension.mapper.ExtEngineUserRelMapper;
import com.rambler.extension.mapper.ExtSearchEngineMapper;
import com.rambler.extension.vo.ExtUserEngineVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author suhao
 */
@Service
public class ExtSearchEngineService extends ServiceImpl<ExtSearchEngineMapper, ExtSearchEngine> {

    @Resource
    private ExtEngineUserRelMapper extEngineUserRelMapper;

    /**
     * 根据用户ID获取用户搜索引擎列表
     *
     * @param userId 用户ID
     * @return 包含用户搜索引擎列表的通用响应对象
     */
    public CommonResponse<List<ExtUserEngineVO>> getUserEngines(String userId) {
        // 查询用户搜索引擎列表
        List<ExtUserEngineVO> extUserEngineList = baseMapper.selectUserSearchEngines(userId);
        return CommonResponse.success(extUserEngineList);
    }

    /**
     * 删除用户搜索引擎关系
     * @param userId 用户ID
     * @param engineId 搜索引擎ID
     * @return 删除结果，成功返回CommonResponse.success()，失败返回CommonResponse.failure()
     */
    public CommonResponse<Void> deleteEngine(String userId, String engineId) {
        // 删除用户搜索引擎关系
        LambdaQueryWrapper<ExtEngineUserRel> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(ExtEngineUserRel::getUserId, userId)
                .eq(ExtEngineUserRel::getEngineId, engineId);
        int result = extEngineUserRelMapper.delete(deleteWrapper);
        if (result == 0) {
            return CommonResponse.failure("删除用户搜索引擎关系失败");
        }
        return CommonResponse.success();
    }


}
